﻿    var baseUrl = 'http://' + document.location.host;
var fileUrl = '../Files/';


function LoadPage()
{
    LoadUsers();
}

function LoadUsers()
{
    var loadUsersUrl = baseUrl + "/FilesAndFoldersService.svc/GetAllUsers";
    $.ajax({
               type: "POST",
               async: true,
               url: loadUsersUrl,
               data: null,
               contentType: "application/json; charset=utf-8",
               success: function (result)
               {
                   for (var i = 0; i < result.length; i++)
                   {
                       $('#UserSelect').append('<option>' + result[i] + '</option>');
                   }
               },
               error: function (xhr, ajaxOptions, thrownError)
               {
                   alert(xhr.status);
                   alert(thrownError);
               }
           });
}

function ChooseUser()
{
    var getUserFolderUrl = baseUrl + "/FilesAndFoldersService.svc/GetUserFolder";
    var user = $('#UserSelect option:selected').text();
    var parentFolderID;
    if (user != "")
    {
        $.ajax({
            type: "POST",
            async: true,
            url: getUserFolderUrl,
            data: JSON.stringify(user),
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                $('#FolderBox').empty();
                $('#FolderBox').append(result.FolderName);
                $('#FileBox').empty();
                $('#ParentFolderID').remove();
                $('#UserBox').append("<p id='ParentFolderID' style='display:none'>" + result.FolderID + "</p>");
                var upButton = $("<button> Up </button>");
                upButton.click(function () {
                    GoUp()
                });
                $('#FolderBox').append(upButton);
                parentFolderID = result.FolderID.toString();

                GetFoldersAndFiles(parentFolderID);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }
        });
    }
}
  
function GetFoldersAndFiles(parentFolderID)
{
    var getFolderFolders = baseUrl + "/FilesAndFoldersService.svc/GetFolderFolders";
    $.ajax({
        type: "POST",
        async: true,
        url: getFolderFolders,
        data: JSON.stringify(parentFolderID),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            $('#FolderBox').append("<p>Folders <input type=text id='FolderName'><button id='NewFolderButton'>Add Folder</button></p>");
            $('#NewFolderButton').click(function () {
                CreateFolder()
            });

            for (var i = 0; i < result.length; i++) {
                var folderName = result[i].FolderName;
                var folderID = result[i].FolderID;
                var folder = $("<button id='Folder" + folderID + "'>" + folderName + "</button>");
                folder.click(function () {
                    SelectFolder($(this).attr("id"), $(this).text())
                });
                $('#FolderBox').append(folder)
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });

    var getFolderFiles = baseUrl + "/FilesAndFoldersService.svc/GetFolderFiles";
    $.ajax({
               type: "POST",
               async: true,
               url: getFolderFiles,
               data: JSON.stringify(parentFolderID),
               contentType: "application/json; charset=utf-8",
               success: function (result)
               {
                   $('#FileBox').append("<p>Files</p>");
                   for (var i = 0; i < result.length; i++)
                   {
                       $('#FileBox').append("| <a href='" + fileUrl + result[i].FolderName + "'>" + result[i].FolderName + "</a> |");
                   }
               },
               error: function (xhr, ajaxOptions, thrownError)
               {
                   alert(xhr.status);
                   alert(thrownError);
               }
           });
}

function SelectFolder(folderID, folderName) {
    var folderTrueID = folderID.substring(6);

    $('#FolderBox').empty();
    $('#FolderBox').append(folderName);
    $('#FileBox').empty();
    $('#ParentFolderID').remove();
    $('#UserBox').append("<p id='ParentFolderID' style='display:none'>" + folderTrueID + "</p>");
    var upButton = $("<button> Up </button>");
    upButton.click(function () {
        GoUp()
    });
    $('#FolderBox').append(upButton);
    GetFoldersAndFiles(folderTrueID);
}

function GoUp() {
    var folderID = $('#ParentFolderID').text();
    var parentFolderID;

    var goUp = baseUrl + "/FilesAndFoldersService.svc/GetParentFolder";
    $.ajax({
        type: "POST",
        async: true,
        url: goUp,
        data: JSON.stringify(folderID),
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            $('#FolderBox').empty();
            $('#FolderBox').append(result.FolderName);
            $('#FileBox').empty();
            $('#ParentFolderID').remove();
            $('#UserBox').append("<p id='ParentFolderID' style='display:none'>" + result.FolderID + "</p>");
            var upButton = $("<button> Up </button>");
            upButton.click(function () {
                GoUp()
            });
            $('#FolderBox').append(upButton);
            parentFolderID = result.FolderID.toString();

            GetFoldersAndFiles(parentFolderID);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Top folder reached!");
        }
    });
}

function CreateFolder() {
    var folderName = $('#FolderName').val();
    if (folderName=="") {
        alert("You must choose a name!");
    }
    else {
        var parentFolderID = $('#ParentFolderID').text()
        var createFolder = baseUrl + "/FilesAndFoldersService.svc/CreateFolder";
        var folderFolder =
        {
        FolderName : folderName,
        FolderID : parentFolderID
        };
        $.ajax({
            type: "POST",
            async: true,
            url: createFolder,
            data: JSON.stringify(folderFolder),
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                var folderName = result.FolderName;
                var folderID = result.FolderID;
                var folder = $("<button id='Folder" + folderID + "'>" + folderName + "</button>");
                folder.click(function () {
                    SelectFolder($(this).attr("id"), $(this).text())
                });
                $('#FolderBox').append(folder)
                alert("Folder created!");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("Folder could not be created!");
            }
        });
    }
}